package com.wj.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wj.common.R;
import com.wj.entity.Employee;
import com.wj.entity.Orders;
import com.wj.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单信息：{}",orders);
        //调用service的方法
        orderService.submit(orders);
        return R.success("支付成功！");
    }


    /**
     * 订单列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize){
        log.info("page:{},pageSize:{}",page,pageSize);

        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        orderService.page(ordersPage,queryWrapper);
        return R.success(ordersPage);
    }


    /**
     * 管理端订单列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    @SuppressWarnings("all")
    public R<Page> page(int page ,int pageSize){
        log.info("page:{},pageSize:{} ",page,pageSize);
        Page<Orders> pageinfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageinfo,queryWrapper);
        return R.success(pageinfo);
    }
}
